package com.bblocks.common.anti_reptile.rule.impl;


import com.bblocks.common.anti_reptile.config.AntiReptileProperties;
import com.bblocks.common.anti_reptile.config.bean.RuleComm;
import com.bblocks.common.anti_reptile.constant.RuleTypeEnum;
import com.bblocks.common.anti_reptile.rule.AbstractRule;
import eu.bitwalker.useragentutils.DeviceType;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Jin
 * @since 2024/3/23 14:40
 */
@RequiredArgsConstructor
@Slf4j
public class UaRule extends AbstractRule {
    private final RuleTypeEnum ruleType = RuleTypeEnum.UA;

    private final AntiReptileProperties properties;


    @Override
    protected RuleComm doMatch(HttpServletRequest request, HttpServletResponse response) {
        com.bblocks.common.anti_reptile.config.bean.UaRule uaRule = properties.getUaRule();
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        OperatingSystem os = userAgent.getOperatingSystem();
        OperatingSystem osGroup = userAgent.getOperatingSystem().getGroup();
        DeviceType deviceType = userAgent.getOperatingSystem().getDeviceType();
        if (DeviceType.UNKNOWN.equals(deviceType)) {
            log.info("Intercepted request, uri: {} Unknown device, User-Agent: {}" ,request.getRequestURI() , userAgent.toString());
            return uaRule;
        } else if (OperatingSystem.UNKNOWN.equals(os)
                || OperatingSystem.UNKNOWN_MOBILE.equals(os)
                || OperatingSystem.UNKNOWN_TABLET.equals(os)) {
            log.info("Intercepted request, uri: {} Unknown OperatingSystem, User-Agent: {}",request.getRequestURI() , userAgent.toString());
            return uaRule;
        }
        if (!uaRule.isAllowedLinux() && (OperatingSystem.LINUX.equals(osGroup) || OperatingSystem.LINUX.equals(os))) {
            log.info("Intercepted request, uri: {} Not Allowed Linux request, User-Agent: {}" ,request.getRequestURI(), userAgent.toString());
            return uaRule;
        }
        if (!uaRule.isAllowedMobile() && (DeviceType.MOBILE.equals(deviceType) || DeviceType.TABLET.equals(deviceType))) {
            log.info("Intercepted request, uri: {} Not Allowed Mobile Device request, User-Agent: {}" ,request.getRequestURI(), userAgent.toString());
            return uaRule;
        }
        if (!uaRule.isAllowedPc() && DeviceType.COMPUTER.equals(deviceType)) {
            log.info("Intercepted request, uri: {} Not Allowed PC request, User-Agent: {}" ,request.getRequestURI(), userAgent.toString());
            return uaRule;
        }
        if (!uaRule.isAllowedIot() && (DeviceType.DMR.equals(deviceType) || DeviceType.GAME_CONSOLE.equals(deviceType) || DeviceType.WEARABLE.equals(deviceType))) {
            log.info("Intercepted request, uri: {} Not Allowed Iot Device request, User-Agent: {}" ,request.getRequestURI(), userAgent.toString());
            return uaRule;
        }
        if (!uaRule.isAllowedProxy() && OperatingSystem.PROXY.equals(os)) {
            log.info("Intercepted request, uri: {} Not Allowed Proxy request, User-Agent: {}" ,request.getRequestURI(), userAgent.toString());
            return uaRule;
        }
        return null;
    }

    @Override
    public void reset(HttpServletRequest request, String realRequestUri) {
        return;
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
